﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TransportLayer.Manager
{
    public partial class TestEngine : Form
    {
        //private System.Windows.Forms.OpenFileDialog openFileDialog=null;
        private Engine engine;


        private TransportLayer.NodeToolsLibrary.StringTokenizer tokens = new TransportLayer.NodeToolsLibrary.StringTokenizer();
        
        public TestEngine(Engine engine)    
        {
            InitializeComponent();
            this.engine = engine;
            radioButton2.Enabled = false;
            radioButton1.Enabled = false;
        }

        private void sendButton1_Click(object sender, EventArgs e)
        {
            PrepereResponse();  
        }

        private void readTopologyButton1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                engine.Reset();
                nodeIdComboBox.Items.Clear();
                this.DeleteTransportLayer();
                Loader loader = new Loader(engine);
                loader.Show();
                loader.LoadNetwork(openFileDialog.FileName);
                foreach (KeyValuePair<int,Engine.Node> n in engine.Nodes)
                {
                    nodeIdComboBox.Items.Add(n.Key);
                }
                nodeIdComboBox.SelectedIndex = 0;
                radioButton1.Enabled = true;
                radioButton2.Enabled = true;
                radioButton1.Checked = true;
            }
        }

  

        private void sendTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13) //13 ---> enter
            {
                
                if (radioButton2.Checked==true)
                    this.PrepereEngineResponse();
                else if (radioButton1.Checked == true)
                    this.PrepereResponse();
                else 
                    this.PrepereNone();
            }
            outputTextBox.SelectionLength = 0;

            outputTextBox.SelectionStart = outputTextBox.Text.Length;

            outputTextBox.ScrollToCaret();

        }
        private void PrepereNone() 
        {

            tokens.Add(sendTextBox.Text);
            outputTextBox.Text += "> " + sendTextBox.Text + Environment.NewLine;
            sendTextBox.Text = "";
    
                outputTextBox.Text += "< Unknown comand" + Environment.NewLine; 
            tokens.End();
        }

        private void PrepereEngineResponse()
        {
            string[] commands = sendTextBox.Text.Split(' ');
            tokens.Add(sendTextBox.Text);
            outputTextBox.Text += "> " + sendTextBox.Text + Environment.NewLine;
            sendTextBox.Text = "";
            tokens.End();

            if (commands[0] == "set")
            {
                engine.AddForwardingEntry(4, ">0 1");
                engine.AddForwardingEntry(4, 2, ">0", 2);
            }
            else if (commands[0] == "get" && commands[1] == "nodes")
            {
                foreach (KeyValuePair<int, Engine.Node> n in engine.Nodes)
                {
                    outputTextBox.Text += "< Node ID: " + n.Key + " Name: " + n.Value.Name + " Type: " + n.Value.Type + Environment.NewLine;
                }

            }
            else if (commands[0] == "getupportsin")
            {
                engine.GetUpPortIn(Int32.Parse(commands[1]));
            }
            else if (commands[0] == "getupportsout")
            {
                engine.GetUpPortOut(Int32.Parse(commands[1]));
            }
            else if (commands[0] == "getdownportsin")
            {
                engine.GetDownPortIn(Int32.Parse(commands[1]));
            }
            else if (commands[0] == "getdownportsout")
            {
                engine.GetDownPortOut(Int32.Parse(commands[1]));
            }
            else if (commands[0] == "hideall")
                engine.HideAllNodes();
            else if (commands[0] == "addsink")
            {
                TransportLayer.Node sink = new TransportLayer.Node();
                sink.Id = 5;
                sink.Name = "Sink_5";
                TransportLayer.PortIn p = new PortIn(0);
                p.Id = 0;
                List<TransportLayer.PortIn> plist = new List<TransportLayer.PortIn>();
                plist.Add(p);
                sink.PortsIn = plist;
                sink.PositionX = 300;
                sink.PositionY = 300;
                sink.Type = "Sink";
                //engine.AddSink(sink, 3, 1234);
                sink.save("sss.xml");

            }
     
        }
        private void PrepereResponse()
        {
            outputTextBox.Text += "> " + sendTextBox.Text + Environment.NewLine;
            string response = engine.Query((int)nodeIdComboBox.SelectedItem, sendTextBox.Text) + Environment.NewLine;
            outputTextBox.Text += "< " + response;
            tokens.Add(sendTextBox.Text);
            sendTextBox.Text = "";
            tokens.End();
        }


        private void sendTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
            if (tokens.MovePrevious())
              sendTextBox.Text = tokens.Current;
            }
            else if (e.KeyCode == Keys.Down)
            { 
                 if (tokens.MoveNext())
                     sendTextBox.Text = tokens.Current;
                 else sendTextBox.Text = "";
            }
    
        }

        private void TestEngine_Load(object sender, EventArgs e)
        {

        }

        private void TestEngine_FormClosing(object sender, FormClosingEventArgs e)
        {

            this.DeleteTransportLayer();
        }

        private void DeleteTransportLayer()
        {
            Loader loader = new Loader(engine);
            loader.Show();
            loader.DeleteNetwork();  
        }

  
         
    }
}
